$(document).ready(function() {
	document.getElementById("finalsubmit").disabled = true; 
	
	// tooltip demo
    //$('.tooltip-demo').tooltip({
    //  selector: "a[rel=tooltip]"
    //})

   	$('a[rel=tooltip]').tooltip();

    //$('.tooltip-test').tooltip()


});

var validationComplete = false;

function submitOK(){
	return validationComplete;
}

function validate(){
	
	var rows = $("#inputform tbody tr").length;
	var correct = 0;
	$("#inputform tbody tr").each( function(i, elem){
		var row = elem;
		var valStr = $(elem).children(".ctr-val").children(":input").val().replace(',', '').replace('.', '');
		var val = parseFloat(valStr);
		var foreStr = $(elem).children('.ctr-forecast').text().replace(',', '').replace('.', '');
		var forecast = parseFloat(foreStr);
		var comment = $(elem).children('.ctr-comment').children('span');
		
		var lower = 0.8 * forecast;
		var upper = 1.2 * forecast;
		
		if(val >= lower && val <= upper){
			correct += 1;
			$(comment).attr('class', 'label label-success');
			$(comment).text('Validation successful');
			$(elem).attr('class', 'success');
			$(elem).children('.ctr-comment').children('.ctr-justify').css('visibility', 'hidden');
			$(elem).children('.ctr-comment').children('.ctr-justify').children('div').collapse('hide');
		}
		else{
			$(comment).attr('class', 'label label-important');
			$(comment).text('Validation failed - value differs greatly from forecast');
			$(elem).attr('class', 'error');
			var foo = $(elem).children('.ctr-comment').children('.ctr-justify');
			$(elem).children('.ctr-comment').children('.ctr-justify').css('visibility', 'visible');
		}
		
	});
	/*
	if(rows === correct){
		document.getElementById("finalsubmit").disabled = false; 
		$("#finalsubmit").attr('class', 'btn btn-primary' );
		validationComplete = true;
	}
	*/
	validateAll();
	
}


function validateAll(){
	var success = true;
	$("#inputform tbody tr").each( function(i, elem){
		var cl =$(elem).attr('class'); 
		if(cl !== 'warning' && cl !== 'success'){
			success = false;
		}
	});
	
	if(success){
		document.getElementById("finalsubmit").disabled = false; 
		$("#finalsubmit").attr('class', 'btn btn-primary' );
		validationComplete = true;
	}
}


function saveInputs(obj){
	var row = $(obj).parents('tr');
	var textarea = $(obj).siblings('textarea');
	var inp = $(textarea).val();
	var justify = $(row).children('.ctr-comment').children('.ctr-justify');
	var textcomment = $(justify).children('.collapse').children('.ctr-textcomment');
	var butsave = $(justify).children('.collapse').children('.butsave');
	var butedit = $(justify).children('.collapse').children('.butedit');
	if(inp.length > 5){
		$(row).attr('class', 'warning');
		var comment = $(row).children('.ctr-comment').children('span');
		$(comment).attr('class', 'label label-warning');
		$(comment).text('Reason for large deviation given.');
		$(textcomment).text(inp);
		$(textcomment).css('display', 'inline-block');
		$(textarea).css('display', 'none');
		$(butsave).css('display', 'none');
		$(butedit).css('display', 'inline-block');
		
		validateAll();
	}
	
}

function switchEdit(obj){
	var row = $(obj).parents('tr');
	var textarea = $(obj).siblings('textarea');
	var inp = $(textarea).val();
	var justify = $(row).children('.ctr-comment').children('.ctr-justify');
	var textcomment = $(justify).children('.collapse').children('.ctr-textcomment');
	var butsave = $(justify).children('.collapse').children('.butsave');
	var butedit = $(justify).children('.collapse').children('.butedit');
	if(inp.length > 5){
		$(textarea).text(inp);
		$(textarea).css('display', 'inline-block');
		$(textcomment).css('display', 'none');
		$(butsave).css('display', 'inline-block');
		$(butedit).css('display', 'none');
		
		validateAll();
	}
	
}

function resetRow(obj){
	var row = $(obj).parents('tr');
	var comment = $(row).children('.ctr-comment').children('span');
	
	$(row).removeClass('error');
	$(row).removeClass('warning');
	$(row).removeClass('success');
	$(comment).attr('class', 'label');
	$(comment).text('Validation required');
	
	$(row).children('.ctr-comment').children('.ctr-justify').css('visibility', 'hidden');
}
